//
//  QDCaseManagerController.m
//  QuickDoctor
//
//  Created by ycw on 2016/12/21.
//  Copyright © 2016年 Eric. All rights reserved.
//

#import "QDCaseManagerController.h"
#import "QDAddCaseController.h"
#import "QDCaseManagerCell1.h"
#import "QDCaseManagerCell2.h"
#import "QDCaseModel.h"

static NSString *cellID1 = @"case1";

static NSString *cellID2 = @"case2";

@interface QDCaseManagerController ()<UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic) NSMutableArray<QDCaseModel *> *muArr;

@property(weak,nonatomic) UITableView *tableView;

@end

@implementation QDCaseManagerController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"病例管理";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUpUI];
    
    [self createFooterView];
    
    [self loadData];
    
}

#pragma mark
#pragma mark - 搭建界面
- (void) setUpUI
{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    
    [self.view addSubview:tableView];
    
    self.tableView = tableView;
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
        
    }];
    
    tableView.delegate = self;
    
    tableView.dataSource = self;
    
    tableView.estimatedRowHeight = 150;
    
    tableView.rowHeight = UITableViewAutomaticDimension;
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [tableView registerClass:[QDCaseManagerCell1 class] forCellReuseIdentifier:cellID1];
    
    [tableView registerClass:[QDCaseManagerCell2 class] forCellReuseIdentifier:cellID2];
    
    tableView.tableHeaderView = [self createHeaderView];
    
    tableView.tableFooterView = [UIView new];
    
}

#pragma mark
#pragma mark - 加载数据
- (void) loadData
{
    //获取文件路径
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:[NSString stringWithFormat:@"ycw_illnessHistory.plist"]];
    //进行解档
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
    _muArr = [array mutableCopy];
    
    [self.tableView reloadData];
    
}




#pragma mark
#pragma mark - 创建上部视图
- (UIView *) createHeaderView
{
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
    
    UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"good_image_default"]];
    
    [headerView addSubview:imgV];
    
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.top.equalTo(headerView).mas_offset(20);
        
        make.height.width.mas_equalTo(100);
        
    }];
    
    UILabel *lblName = [[UILabel alloc] init];
    
    lblName.text = @"王二狗";
    
    lblName.textAlignment = NSTextAlignmentCenter;
    
    lblName.font = [UIFont systemFontOfSize:17];
    
    [headerView addSubview:lblName];
    
    [lblName mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(imgV);
        
        make.top.equalTo(imgV.mas_bottom).mas_offset(10);
        
        make.bottom.equalTo(headerView).mas_offset(-20);
        
        make.trailing.equalTo(imgV);
        
    }];
    
    UIImageView *imgIcon1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"0"]];
    
    [headerView addSubview:imgIcon1];
    
    [imgIcon1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(imgV.mas_trailing).mas_offset(10);
        
        make.top.equalTo(imgV);
        
        make.height.width.mas_equalTo(20);
        
    }];
    
    UILabel *lblText1 = [[UILabel alloc] init];
    
    lblText1.text = @"男   28";
    
    [headerView addSubview:lblText1];
    
    [lblText1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(imgIcon1.mas_trailing).mas_offset(10);
        
        make.top.bottom.equalTo(imgIcon1);
        
        make.trailing.equalTo(headerView).mas_offset(-10);
        
    }];
    
    UIImageView *imgIcon2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"idCard"]];
    
    [headerView addSubview:imgIcon2];
    
    [imgIcon2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(imgIcon1);
        
        make.top.equalTo(imgIcon1.mas_bottom).mas_offset(10);
        
        make.height.width.mas_equalTo(20);
        
    }];
    
    UILabel *lblText2 = [[UILabel alloc] init];
    
    lblText2.text = @"2343243242343";
    
    [headerView addSubview:lblText2];
    
    [lblText2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(lblText1);
        
        make.top.bottom.equalTo(imgIcon2);
        
        make.trailing.equalTo(headerView).mas_offset(-10);
        
    }];
    
    UIImageView *imgIcon3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"phone"]];
    
    [headerView addSubview:imgIcon3];
    
    [imgIcon3 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(imgIcon1);
        
        make.top.equalTo(imgIcon2.mas_bottom).mas_offset(10);
        
        make.height.width.mas_equalTo(20);
        
    }];
    
    UILabel *lblText3 = [[UILabel alloc] init];
    
    lblText3.text = @"13157132432";
    
    [headerView addSubview:lblText3];
    
    [lblText3 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(lblText1);
        
        make.top.bottom.equalTo(imgIcon3);
        
        make.trailing.equalTo(headerView).mas_offset(-10);
        
    }];
    
    UIImageView *imgIcon4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"illness_rb_img_sel.png"]];
    
    [headerView addSubview:imgIcon4];
    
    [imgIcon4 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(imgIcon1);
        
        make.top.equalTo(imgIcon3.mas_bottom).mas_offset(10);
        
        make.height.width.mas_equalTo(20);
        
    }];
    
    UILabel *lblText4 = [[UILabel alloc] init];
    
    lblText4.text = @"上海市";
    
    [headerView addSubview:lblText4];
    
    [lblText4 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(lblText1);
        
        make.top.bottom.equalTo(imgIcon4);
        
        make.trailing.equalTo(headerView).mas_offset(-10);
        
    }];
    
    return  headerView;
    
}


#pragma mark
#pragma mark - 创建表尾视图
- (void) createFooterView
{
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 600, [UIScreen mainScreen].bounds.size.width, 60)];
    
    [self.view addSubview:footerView];
    
    UIButton *button = [[UIButton alloc] init];
    
    [button setTitle:@"添加病例" forState:UIControlStateNormal];
    
    [button setBackgroundColor:[UIColor colorWithRed:23/255.0 green:188/255.0 blue:187/255.0 alpha:1]];
    
    [footerView addSubview:button];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(footerView);
        
        make.leading.equalTo(footerView).mas_offset(10);
        
        make.bottom.trailing.equalTo(footerView).mas_offset(-10);
        
    }];
    
    [button addTarget:self action:@selector(clickAddButton) forControlEvents:UIControlEventTouchUpInside];
    
}


#pragma mark
#pragma mark - 实现数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.muArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    QDCaseModel *model = self.muArr[indexPath.row];

    
    if (model.illnessImage == nil) {
        QDCaseManagerCell1 *cell = [tableView dequeueReusableCellWithIdentifier:cellID1 forIndexPath:indexPath];
        
        cell.model = model;
        
        return cell;
    }else{
        
        QDCaseManagerCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:cellID2 forIndexPath:indexPath];
        
        cell2.model = model;
        
        return cell2;
    }
    
    return nil;
    
}

#pragma mark
#pragma mark - 实现点击button跳转
- (void) clickAddButton
{
    
    __weak QDCaseManagerController *weakSelf = self;
    
    QDAddCaseController *addC = [[QDAddCaseController alloc] init];
    
    addC.completeBlock = ^(QDCaseModel *model){
        
        [self loadData];
        
        
        
        [weakSelf.tableView reloadData];
    };
    
    [self.navigationController pushViewController:addC animated:YES];
    
}




@end
